package com.it.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.it.service.IQuestionBankService;
import com.it.util.Result;
import com.it.vo.AnswerVo;
import com.it.vo.QuestionInfoVo;
import com.it.vo.QuestionVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-23
 */
@RestController
@RequestMapping("/questionBank")
public class QuestionBankController {

    @Resource
    IQuestionBankService questionBankService;

    @PostMapping("/addQuestion")
    Result<String> addQuestion(@RequestBody QuestionVo questionVo){
        System.out.println(questionVo);
        questionBankService.addQuestion( questionVo);
        return Result.success();
    }

    @GetMapping("/getQuestionList")
    Result<Map<String,Object>> getQuestionList(){
        Map<String, Object> questionInfo = questionBankService.getQuestionList();
        return Result.success(questionInfo);
    }

    @PostMapping("/answerQuestion")
    Result answerQuestion(@RequestBody Map<String, Object> request){
        List<Object> list = (List<Object>) request.get("answers");
        HashMap<Long, String> answers=new HashMap<Long, String>();
        list.forEach(answer->{
            AnswerVo answerVo = JSON.parseObject(JSON.toJSONString(answer), AnswerVo.class);
            answers.put(answerVo.getQuestionId(), answerVo.getAnswer());
        });
        // 从请求中获取考试ID和答案数组
        AtomicLong score = questionBankService.checkAnswer(Long.valueOf((String) request.get("examId")), answers);
        return Result.success(score);
    }
}
